<?php
   /**
    * OrangeHRM Enterprise is a closed sourced comprehensive Human Resource Management (HRM)
    * System that captures all the essential functionalities required for any enterprise.
    * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
    *
    * OrangeHRM Inc is the owner of the patent, copyright, trade secrets, trademarks and any
    * other intellectual property rights which subsist in the Licensed Materials. OrangeHRM Inc
    * is the owner of the media / downloaded OrangeHRM Enterprise software files on which the
    * Licensed Materials are received. Title to the Licensed Materials and media shall remain
    * vested in OrangeHRM Inc. For the avoidance of doubt title and all intellectual property
    * rights to any design, new software, new protocol, new interface, enhancement, update,
    * derivative works, revised screen text or any other items that OrangeHRM Inc creates for
    * Customer shall remain vested in OrangeHRM Inc. Any rights not expressly granted herein are
    * reserved to OrangeHRM Inc.
    *
    * You should have received a copy of the OrangeHRM Enterprise  proprietary license file along
    * with this program; if not, write to the OrangeHRM Inc. 538 Teal Plaza, Secaucus , NJ 0709
    * to get the file.
    *
    */
   
   require_once ROOT_PATH.'/lib/dao/DMLFunctions.php';
   require_once ROOT_PATH.'/lib/dao/SQLQBuilder.php';
   
   class LdapDetails {
   
   	private $ldapServer;
   	private $ldapPort;
   	private $ldapDomainName;
   	private $ldapStatus;
   	const LDAP_TYPE = "Open LDAP";
   
   	public function setLdapServer($ldapServer) {
   		$this->ldapServer = $ldapServer;
   	}
   
   	public function getLdapServer() {
   		return $this->ldapServer;
   	}
   
   	public function setLdapPort($ldapPort) {
   		$this->ldapPort = $ldapPort;
   	}
   
   	public function getLdapPort() {
   		return $this->ldapPort;
   	}
   
   	public function setLdapDomainName($ldapDomainName) {
   		$this->ldapDomainName = $ldapDomainName;
   	}
   
   	public function getLdapDomainName() {
   		return $this->ldapDomainName;
   	}
   
   	public function setLdapStatus($ldapStatus) {
   		$this->ldapStatus = $ldapStatus;
   	}
   
   	public function getLdapStatus() {
   		return $this->ldapStatus;
   	}
   
   	private function _updateLdap($key, $value) {
   
   		$sqlBuilder = new SQLQBuilder();
   
   		$updateFields[0] = '`value`';
   		$updateValues[0] = "'" . $value . "'";
   
   		$updateTable = '`hs_hr_config`';
   
   		$updateConditions[0] = "`key` = '". $key ."'";
   
   		$query = $sqlBuilder->simpleUpdate($updateTable, $updateFields, $updateValues, $updateConditions);
   
   		$dbConnection = new DMLFunctions();
   
   		if($dbConnection->executeQuery($query)) {
   			return true;
   		} else {
   			return false;
   		}
   
   	}
   	
   
   	public function updateLdapDetails() {
   
   		$result1 = $this->_updateLdap("ldap_server", $this->getLdapServer());
   		$result2 = $this->_updateLdap("ldap_port", $this->getLdapPort());
   		$result3 = $this->_updateLdap("ldap_domain_name", $this->getLdapDomainName());
   		$result4 = $this->_updateLdap("ldap_status", $this->getLdapStatus());
   
   		if ($result1 && $result2 && $result3 && $result4) {
   			return true;
   		} else {
   			return false;
   		}
   	}
   
   	public function retrieveLdapDetails() {
   
   		$sqlBuilder = new SQLQBuilder();
   
   		$selectFields[0] = '`key`';
   
   		$selectFields[1] = '`value`';
   
   		$selectTable = '`hs_hr_config`';
   
   		$selectConditions[0] = "`key` LIKE 'ldap%'";
   
   		$query = $sqlBuilder->simpleSelect($selectTable, $selectFields, $selectConditions);
   
   		$dbConnection = new DMLFunctions();
   
   		$result = $dbConnection->executeQuery($query);
   
   		while ($row = $dbConnection->dbObject->getArray($result)) {
   			$key = $row['key'];
   			$value["$key"] = $row['value'];
   		}
   
   		$this->setLdapServer($value['ldap_server']);
   		$this->setLdapDomainName($value['ldap_domain_name']);
   		$this->setLdapPort($value['ldap_port']);
   		$this->setLdapStatus($value['ldap_status']);
   
   		return $this;
   	}
   
   
   }
   
   ?>
